package com.ruoyi.common.utils;

import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatUtil {

    static DecimalFormat df1 = new DecimalFormat ("0");
    static DecimalFormat df2 = new DecimalFormat ("0.0");
    public static String format(String value){
        String newValue = value;
        if(newValue.indexOf(".") > 0){//对小数进行格式化
            try{
                DecimalFormat df = new DecimalFormat ("0.0");
                double tempValue = Double.valueOf(value);
                newValue = df.format(tempValue);
            }catch (Exception e){
                String suffix = newValue.substring(newValue.indexOf(".") + 1);
                if(StringUtils.hasLength(suffix)){
                    if(suffix.length() > 1){
                        suffix = suffix.substring(0,2);
                    }else{
                        suffix = suffix.substring(0,1);
                    }
                }
                newValue = StringUtils.hasLength(suffix) ? (newValue.substring(0,newValue.indexOf(".") + 1) + suffix) : newValue.substring(0,newValue.indexOf("."));
            }
        }else{
            //整型以及十六进制字符的处理
        }
        return newValue;
    }

    public static String formatTwo(String value){
        String newValue = value;
        if(newValue.indexOf(".") > 0){//对小数进行格式化
            try{
                DecimalFormat df = new DecimalFormat ("0.00");
                double tempValue = Double.valueOf(value);
                newValue = df.format(tempValue);
            }catch (Exception e){
                String suffix = newValue.substring(newValue.indexOf(".") + 1);
                if(StringUtils.hasLength(suffix)){
                    if(suffix.length() > 1){
                        suffix = suffix.substring(0,2);
                    }else{
                        suffix = suffix.substring(0,1);
                    }
                }
                newValue = StringUtils.hasLength(suffix) ? (newValue.substring(0,newValue.indexOf(".") + 1) + suffix) : newValue.substring(0,newValue.indexOf("."));
            }
        }else{
            //整型以及十六进制字符的处理
        }
        return newValue;
    }

    public static String formatThree(String value){
        String newValue = value;
        if(newValue.indexOf(".") > 0){//对小数进行格式化
            try{
                DecimalFormat df = new DecimalFormat ("0.000");
                double tempValue = Double.valueOf(value);
                newValue = df.format(tempValue);
            }catch (Exception e){
                String suffix = newValue.substring(newValue.indexOf(".") + 1);
                if(StringUtils.hasLength(suffix)){
                    if(suffix.length() >= 3){
                        suffix = suffix.substring(0,3);
                    }else if(suffix.length() == 2){
                        suffix = suffix.substring(0,2);
                    }else if(suffix.length() == 1){
                        suffix = suffix.substring(0,1);
                    }
                }
                newValue = StringUtils.hasLength(suffix) ? (newValue.substring(0,newValue.indexOf(".") + 1) + suffix) : newValue.substring(0,newValue.indexOf("."));
            }
        }else{
            //整型以及十六进制字符的处理
        }
        return newValue;
    }

    public static String formatFour(String value){
        String newValue = value;
        if(newValue.indexOf(".") > 0){//对小数进行格式化
            try{
                DecimalFormat df = new DecimalFormat ("0.0000");
                double tempValue = Double.valueOf(value);
                newValue = df.format(tempValue);
            }catch (Exception e){
                String suffix = newValue.substring(newValue.indexOf(".") + 1);
                if(StringUtils.hasLength(suffix)){
                    if(suffix.length() >= 4){
                        suffix = suffix.substring(0,4);
                    }else if(suffix.length() == 3){
                        suffix = suffix.substring(0,3);
                    }else if(suffix.length() == 2){
                        suffix = suffix.substring(0,2);
                    }else if(suffix.length() == 1){
                        suffix = suffix.substring(0,1);
                    }
                }
                newValue = StringUtils.hasLength(suffix) ? (newValue.substring(0,newValue.indexOf(".") + 1) + suffix) : newValue.substring(0,newValue.indexOf("."));
            }
        }else{
            //整型以及十六进制字符的处理
        }
        return newValue;
    }

    /**
     * Int 16位有符号整形
     * DINT 32位有符号整形
     * WORD 16位无符号整形
     * DWORD 32位无符号整形
     * Real 32位浮点数
     * LReal 64位浮点数
     * Bool 布尔型
     *
     * @param value
     * @param valueType
     * @return
     */
    public static String format(String value, Integer valueType){
        //valuetype;//7种类型固定：1、离散型(Bool)，2、长整型(DINT)，3、模拟型(Int)，4、字符型(WORD)，5、浮点型(Real)，6、64位浮点型(LReal)，7、32位无符号整型(DWORD)）
        String newValue = value;
        switch (valueType) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 7:
                try{
                    newValue = df1.format(Double.valueOf(value));
                }catch (Exception e){
                    if(newValue.contains(".")){
                        newValue = newValue.substring(0,newValue.indexOf("."));
                    }
                }
                break;
            case 5:
            case 6:
                try{
                    newValue = df2.format(Double.valueOf(value));
                }catch (Exception e){
                    if(newValue.contains(".")){
                        String suffix = newValue.substring(newValue.indexOf(".") + 1);
                        if(StringUtils.hasLength(suffix)){
                            if(suffix.length() > 1){
                                suffix = suffix.substring(0,2);
                            }else{
                                suffix = suffix.substring(0,1);
                            }
                        }
                        newValue = StringUtils.hasLength(suffix) ? (newValue.substring(0,newValue.indexOf(".") + 1) + suffix) : newValue.substring(0,newValue.indexOf("."));
                    }
                }
                break;
            case 8:
                break;
            default:
        }
        return newValue;
    }

    /**
     *
     * @param name
     * @return
     */
    public static String getCharAndNumber(String name){
        String patternStr = "\\d+.\\d+|\\w+";
        return getCharAndNumberFromStr(name,"-",patternStr);
    }

    public static String getNumber(String name){
        String patternStr = "(\\d+\\.\\d+)|(\\d+)";
        return getCharAndNumberFromStr(name,"",patternStr);
    }

    public static String getCharAndNumberFromStr(String name,String split,String patternStr){
        Pattern pattern = Pattern.compile(patternStr);
        Matcher ma = pattern.matcher(name);
        StringBuilder sb = new StringBuilder();
        while(ma.find()){
            sb.append(ma.group() + split);
        }
        return sb.toString();
    }

    /**
     * 字符串是否包含中文
     *
     * @param str 待校验字符串
     * @return true 包含中文字符 false 不包含中文字符
     *
     */
    public static boolean isContainChinese(String str) {
        if (!StringUtils.hasLength(str)) {
            return false;
        }
        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static int getLenOfNumber(String item){
        if(StringUtils.hasLength(item)){
            int len = item.length();
            for(int i=0;i<len;i++){
                String firstStr = item.substring(i,i+1);
                boolean isChinese = FormatUtil.isContainChinese(firstStr);
                boolean isNumber = isNumber(firstStr);
                boolean isEnglish = isString(firstStr);
                if(isNumber){
                    //如果是数字，则继续往后查询
                    continue;
                }else if(isChinese){
                    //第i位是中文，说明i位前面是数字
                    return i;
                }else if(isEnglish){
                    //如果不是中文，看看是否是英文，如果是英文，那么前面就是数字
                    return i;
                }
            }
        }
        return 2;//默认第二位是中文，第一位就是数字
    }

    public static int getLenOfNumberExcludeEnglish(String item){
        if(StringUtils.hasLength(item)){
            int len = item.length();
            for(int i=0;i<len;i++){
                String firstStr = item.substring(i,i+1);
                boolean isChinese = FormatUtil.isContainChinese(firstStr);
                boolean isNumber = isNumber(firstStr);
                boolean isEnglish = isString(firstStr);
                if(isNumber){
                    //如果是数字，则继续往后查询
                    continue;
                }else if(isChinese){
                    //第i位是中文，说明i位前面是数字
                    return i;
                }else if(isEnglish){
                    //如果不是中文，看看是否是英文，如果是英文，那么前面就是数字
                    continue;
                }
            }
        }
        return -1;//默认
    }

    public static boolean isString(String s) {
        return s.matches("^[a-zA-Z]*");
    }

    public static boolean isNumber(String s) {
        Pattern pattern = Pattern.compile("^[0-9]*");
        return pattern.matcher(s).matches();
    }

}
